import {postForm, get} from '@request'
import {ResType} from "st-common-req"
import {useUserStore} from '@user'


const API_PRE = '/login'

/**
 * 密码登录
 *
 * @param {string} username 用户名
 * @param {string} password 密码
 * @returns {Promise<axios.AxiosResponse<ResType>>} 密码登录请求结果
 */
export const loginByPwd = async (username: string, password: string): Promise<ResType> => {
  return await postForm(
    API_PRE + '/pwd',
    {
      username,
      password
    }
  )
}

/**
 * 获取用户登录是否有效
 *
 * @returns {Promise<ResType>} 登录是否有效请求结果
 */
export const getLoginEffective = async (): Promise<ResType> => {
  // 用户相关信息存储仓库
  const userStore = useUserStore()
  // 获取登录是否有效
  return await get(
    API_PRE + '/effective',
    {
      loginCredential: userStore.getLoginCredential()
    }
  )
}
